package com.michael.demos.springboot.mybatisplus.customizedId.generator;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 自定义ID生成器
 * 仅作为示范
 *
 * @author Michael
 */
@Slf4j
@Component
public class CustomIdGenerator implements IdentifierGenerator {

    @Autowired
    private Snowflake userId;
    @Autowired
    private Snowflake appleId;

    @Override
    public Long nextId(Object entity) {

//        int abcdefg = 1;
        String className = entity.getClass().getName();
        // 根据不同的 className 使用不同的ID生成规则
        log.info(className);

        final String userClass = "com.michael.demos.springboot.mybatisplus.customizedId.entity.User";
        final String appleClass = "com.michael.demos.springboot.mybatisplus.customizedId.entity.Apple";

        switch (className){
            case userClass:
                return userId.nextId();
            case appleClass:
                return appleId.nextId();
            default:
                throw new IllegalStateException("Unexpected value: " + className);
        }
    }
}
